env.setErrorMessageBoxEnabled(true)
local my_ctld = CTLD:New(coalition.side.BLUE, { "BLUE" }, "BLUE")

my_ctld:__Start(5)

-- loadout
my_ctld:AddTroopsCargo("步兵班", { "BLUE GNDFORCETEMPLATE" }, CTLD_CARGO.Enum.TROOPS, 12, nil, nil)
my_ctld:AddCratesCargo("T-90", { "BLUE T90 TEMPLATE" }, CTLD_CARGO.Enum.VEHICLE, 1, 2775, nil)
my_ctld:AddCratesCargo("04A", { "BLUE 04A TEMPLATE" }, CTLD_CARGO.Enum.VEHICLE, 1, 1750, nil)
my_ctld:AddCratesCargo("道尔", { "BLUE Tor TEMPLATE" }, CTLD_CARGO.Enum.VEHICLE, 1, 1751, nil)
my_ctld:AddCratesCargo("通古斯卡", { "BLUE 2S6 TEMPLATE" }, CTLD_CARGO.Enum.VEHICLE, 1, 1752, nil)


-- capabilities
my_ctld:UnitCapabilities("Mi-8MTV2", true, true, 2, 4, 15, 10000)
-- ["Hercules"] = {type="Hercules", crates=true, troops=true, cratelimit = 7, trooplimit = 64}, -- 19t cargo, 64 paratroopers
my_ctld:UnitCapabilities("Hercules", true, true, 7, 64, 15, 19000)

-- zone configurations
my_ctld:AddCTLDZone("LOADZONE-1", CTLD.CargoZoneType.LOAD, SMOKECOLOR.Blue, true, true) -- loadzone1
my_ctld:AddCTLDZone("LOADZONE-2", CTLD.CargoZoneType.LOAD, SMOKECOLOR.Blue, true, true) -- loadzone2
my_ctld:AddCTLDZone("DROPZONE-1", CTLD.CargoZoneType.DROP, nil, true, true) -- DROPZONE
my_ctld:AddCTLDZone("MOVEZONE-1", CTLD.CargoZoneType.MOVE, nil, true, true)
my_ctld:AddCTLDZone("MOVEZONE-2", CTLD.CargoZoneType.MOVE, nil, true, true)
my_ctld:AddCTLDZone("MOVEZONE-3", CTLD.CargoZoneType.MOVE, nil, true, true)
my_ctld:AddCTLDZone("MOVEZONE-4", CTLD.CargoZoneType.MOVE, nil, true, true)
my_ctld:AddCTLDZone("MOVEZONE-5", CTLD.CargoZoneType.MOVE, nil, true, true)

my_ctld.enableHercules = true
my_ctld.forcehoverload = false
my_ctld.HercMinAngels = 155  -- for troop/cargo drop via chute in meters, ca 470 ft
my_ctld.HercMaxAngels = 2000 -- for troop/cargo drop via chute in meters, ca 6000 ft
my_ctld.HercMaxSpeed = 77    -- 77mps or 270kph or 150kn
my_ctld.useprefix = true     -- this is true by default and MUST BE ON.

-- FOB
my_ctld:AddCratesCargo("FARP", { "FOB" }, CTLD_CARGO.Enum.FOB, 2)
local FARPFreq = 130
local FARPName = 1        -- numbers 1..10

local FARPClearnames = {
    [1] = "London",
    [2] = "Dallas",
    [3] = "Paris",
    [4] = "Moscow",
    [5] = "Berlin",
    [6] = "Rome",
    [7] = "Madrid",
    [8] = "Warsaw",
    [9] = "Dublin",
    [10] = "Perth",
}

function BuildAFARP(Coordinate)
    local coord = Coordinate        -- Core.Point#COORDINATE

    local FarpName = ((FARPName - 1) % 10) + 1
    local FName = FARPClearnames[FarpName]

    FARPFreq = FARPFreq + 1
    FARPName = FARPName + 1

    -- Create a SPAWNSTATIC object from a template static FARP object.
    local SpawnStaticFarp = SPAWNSTATIC:NewFromStatic("Static Invisible FARP-1", country.id.USA)

    -- Spawning FARPs is special in DCS. Therefore, we need to specify that this is a FARP. We also set the callsign and the frequency.
    SpawnStaticFarp:InitFARP(FARPName, FARPFreq, 0)
    SpawnStaticFarp:InitDead(false)

    -- Spawn FARP
    local ZoneSpawn = ZONE_RADIUS:New("FARP " .. FName, Coordinate:GetVec2(), 160, false)
    local Heading = 0
    local FarpBerlin = SpawnStaticFarp:SpawnFromZone(ZoneSpawn, Heading, "FARP " .. FName)

    -- ATC and services - put them 125m from the center of the zone towards North
    local FarpVehicles = SPAWN:NewWithAlias("FARP Vehicles Template", "FARP " .. FName .. " Technicals")
    FarpVehicles:InitHeading(180)
    local FarpVCoord = coord:Translate(125, 0)
    FarpVehicles:SpawnFromCoordinate(FarpVCoord)

    -- We will put the rest of the statics in a nice circle around the center
    local base = 330
    local delta = 30

    local windsock = SPAWNSTATIC:NewFromStatic("Static Windsock-1", country.id.USA)
    local sockcoord = coord:Translate(125, base)
    windsock:SpawnFromCoordinate(sockcoord, Heading, "Windsock " .. FName)
    base = base - delta

    local fueldepot = SPAWNSTATIC:NewFromStatic("Static FARP Fuel Depot-1", country.id.USA)
    local fuelcoord = coord:Translate(125, base)
    fueldepot:SpawnFromCoordinate(fuelcoord, Heading, "Fueldepot " .. FName)
    base = base - delta

    local ammodepot = SPAWNSTATIC:NewFromStatic("Static FARP Ammo Storage-2-1", country.id.USA)
    local ammocoord = coord:Translate(125, base)
    ammodepot:SpawnFromCoordinate(ammocoord, Heading, "Ammodepot " .. FName)
    base = base - delta

    local CommandPost = SPAWNSTATIC:NewFromStatic("Static FARP Command Post-1", country.id.USA)
    local CommandCoord = coord:Translate(125, base)
    CommandPost:SpawnFromCoordinate(CommandCoord, Heading, "Command Post " .. FName)
    base = base - delta

    local Tent1 = SPAWNSTATIC:NewFromStatic("Static FARP Tent-11", country.id.USA)
    local Tent1Coord = coord:Translate(125, base)
    Tent1:SpawnFromCoordinate(Tent1Coord, Heading, "Command Tent " .. FName)
    base = base - delta

    local Tent2 = SPAWNSTATIC:NewFromStatic("Static FARP Tent-11", country.id.USA)
    local Tent2Coord = coord:Translate(125, base)
    Tent2:SpawnFromCoordinate(Tent2Coord, Heading, "Command Tent2 " .. FName)

    -- add a loadzone to CTLD
    my_ctld:AddCTLDZone("FARP " .. FName, CTLD.CargoZoneType.LOAD, SMOKECOLOR.Blue, true, true)
    local m = MESSAGE:New(string.format("FARP %s in operation!", FName), 15, "CTLD"):ToBlue()
end

function my_ctld:OnAfterCratesBuild(From, Event, To, Group, Unit, Vehicle)
    local name = Vehicle:GetName()
    if string.match(name, "FOB", 1, true) then
        local Coord = Vehicle:GetCoordinate()
        Vehicle:Destroy(false)
        BuildAFARP(Coord)
    end
end

